package com.magictower.primary.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.magictower.primary.R;
import com.magictower.primary.bean.TaskInfo;
import com.magictower.primary.common.SPConstant;

import java.util.List;

public class TaskManagerAdapter extends BaseAdapter {

    private List<TaskInfo> userTaskInfos;
    private List<TaskInfo> systemTaskInfos;
    private Context mContext;
    private final SharedPreferences mPref;

    public TaskManagerAdapter(Context context, List<TaskInfo> userTaskInfos, List<TaskInfo> systemTaskInfos) {
        this.mContext = context;
        mPref = this.mContext.getSharedPreferences(SPConstant.CONFIG, Context.MODE_PRIVATE);
        this.userTaskInfos = userTaskInfos;
        this.systemTaskInfos = systemTaskInfos;
    }

    @Override
    public int getCount() {
        /**
         * 判断当前用户是否需要展示系统进程
         * 如果需要就全部展示
         * 如果不需要就展示用户进程
         */
        boolean res = mPref.getBoolean(SPConstant.SHOW_SYSTEM_PROCESS, false);
        if (res){
            return userTaskInfos.size() + 1 + systemTaskInfos.size() + 1;
        } else {
            return userTaskInfos.size() + 1;
        }
    }

    @Override
    public Object getItem(int position) {
        if (position == 0) {
            return null;
        } else if (position == userTaskInfos.size() + 1) {
            return null;
        }
        TaskInfo taskInfo;
        if (position < userTaskInfos.size() + 1) {
            //把多出来的特殊的条目减掉
            taskInfo = userTaskInfos.get(position - 1);
        } else {
            int location = userTaskInfos.size() + 2;
            taskInfo = systemTaskInfos.get(position - location);
        }
        return taskInfo;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //如果当前的position等于0 表示应用程序
        if (position == 0) {
            TextView textView = new TextView(mContext);
            textView.setTextColor(Color.WHITE);
            textView.setBackgroundColor(Color.GRAY);
            textView.setPadding(3,0,3,0);
            textView.setText("用户程序(" + userTaskInfos.size() + ")个");
            return textView;
            //表示系统程序
        } else if (position == userTaskInfos.size() + 1) {
            TextView textView = new TextView(mContext);
            textView.setTextColor(Color.WHITE);
            textView.setBackgroundColor(Color.GRAY);
            textView.setPadding(3,0,3,0);
            textView.setText("系统程序(" + systemTaskInfos.size() + ")个");
            return textView;
        }

        TaskInfo taskInfo;
        if (position < userTaskInfos.size() + 1) {
            //把多出来的特殊的条目减掉
            taskInfo = userTaskInfos.get(position - 1);
        } else {
            int location = userTaskInfos.size() + 2;
            taskInfo = systemTaskInfos.get(position - location);
        }

        View view;
        ViewHolder holder;
        //todo 为什么要加convertView instanceof RelativeLayout
        if (convertView != null && convertView instanceof RelativeLayout) {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        } else {
            view = View.inflate(mContext, R.layout.item_task_manager, null);
            holder = new ViewHolder();
            holder.iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
            holder.tv_apk_size = (TextView) view.findViewById(R.id.tv_apk_size);
            holder.cb_status = (CheckBox) view.findViewById(R.id.cb_status);
            holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
            view.setTag(holder);
        }

        holder.tv_name.setText(taskInfo.getAppName());
        holder.tv_apk_size.setText("占用内存："+Formatter.formatFileSize(mContext.getApplicationContext(), taskInfo.getMemorySize()));
        holder.iv_icon.setImageDrawable(taskInfo.getIcon());

        if (taskInfo.isChecked()) {
            holder.cb_status.setChecked(true);
        } else {
            holder.cb_status.setChecked(false);
        }

        //判断当前展示的item是否是自己的程序。如果是。就把程序给隐藏
        if(taskInfo.getPackageName().equals(mContext.getPackageName())){
            //隐藏
            holder.cb_status.setVisibility(View.INVISIBLE);
        }else{
            //显示
            holder.cb_status.setVisibility(View.VISIBLE);
        }

        return view;
    }

    private static class ViewHolder {
        ImageView iv_icon;
        TextView tv_apk_size;
        CheckBox cb_status;
        TextView tv_name;
    }
}